package com.aisb.manage;

import com.aisb.core.Result;
import com.aisb.core.log.LogOper;
import com.aisb.core.log.LogVisit;
import com.aisb.core.util.DateUtil;
import com.aisb.core.util.file.ExcelUtils;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/manage/notice")
public class NoticeController {
    private static Logger logger = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    NoticeService noticeService;

    @RequestMapping(value = "/index")
    @LogVisit("公告管理列表")
    public String index(Model model) {
        return "manage/notice";
    }

    @RequestMapping(value = "/add/index")
    public String indexAdd(Model model) {
        return "manage/notice_add";
    }

    @RequestMapping(value = "/modify/{noticeId}")
    public String indexModify(Model model, @PathVariable String noticeId) {
        Object o = noticeService.noticeOne(noticeId);
        model.addAttribute("noticeInfo", o);

        return "manage/notice_modify";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(@RequestParam Map param) {
        String pageNumStr = (String) param.get("pageNum");
        String pageSizeStr = (String) param.get("pageSize");

        Integer pageNum = Integer.parseInt(pageNumStr);
        Integer pageSize = Integer.parseInt(pageSizeStr);

        Page page = noticeService.noticeList(param, pageNum, pageSize);

        Map r = new HashMap();
        r.put("total", page.getTotal());
        r.put("rows", page.getResult());

        return new Result(true, r);
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public Result add(@RequestParam Map param) {
        int result = noticeService.noticeAdd(param);

        return new Result(true, result);
    }

    @RequestMapping(value = "/checkexist")
    @ResponseBody
    public Map checkexist(@RequestParam Map param) {
        Map result = new HashMap();

        Object o = noticeService.noticeOne(param);
        if (null == o) {
            result.put("valid", true);
        } else {
            result.put("valid", false);
        }

        return result;
    }

    @RequestMapping(value = "/modify")
    @ResponseBody
    @LogOper("公告修改")
    public Result modify(@RequestParam Map param) {
        int result = noticeService.noticeModify(param);

        return new Result(true, result);
    }

    @RequestMapping(value = "/delete")
    @RequiresPermissions("/manage/notice/delete")
    @ResponseBody
    @LogOper("公告管理，公告删除")
    public Result delete(@RequestParam Map param) {
        int result = noticeService.noticeDelete(param);

        return new Result(true, result);
    }


    @RequestMapping(value = "/online")
    @RequiresPermissions("/manage/notice/modify")
    @ResponseBody
    @LogOper("公告管理，批量上线")
    public Result online(@RequestParam Map param) {
        String noticeIds = (String) param.get("noticeIds");
        String noticeId = (String) param.get("noticeId");
        if (StringUtils.isEmpty(noticeId)) {
            noticeId = "";
        }
        if (StringUtils.isNotBlank(noticeIds)) {
            noticeId = noticeId + "," + noticeIds;
        }
        Set<String> noticeIdSet = new HashSet(Arrays.asList(noticeId.split(",")));

        param.put("status", "1");
        int result = noticeService.noticeModify(noticeIdSet, param);

        return new Result(true, result);
    }

    @RequestMapping(value = "/offline")
    @RequiresPermissions("/manage/notice/modify")
    @ResponseBody
    @LogOper("公告管理，批量下线")
    public Result offline(@RequestParam Map param) {
        String noticeIds = (String) param.get("noticeIds");
        String noticeId = (String) param.get("noticeId");
        if (StringUtils.isEmpty(noticeId)) {
            noticeId = "";
        }
        if (StringUtils.isNotBlank(noticeIds)) {
            noticeId = noticeId + "," + noticeIds;
        }
        Set<String> noticeIdSet = new HashSet(Arrays.asList(noticeId.split(",")));

        param.put("status", "0");
        int result = noticeService.noticeModify(noticeIdSet, param);

        return new Result(true, result);
    }

    @RequestMapping(value = "/export")
    @RequiresPermissions("/manage/notice/download")
    @ResponseBody
    @LogOper("公告管理，数据导出")
    public Result export(@RequestParam Map param) {
        List<Map> rList = noticeService.noticeList(param);

        String fileName = "公告_" + DateUtil.getInstance().getToday("yyyyMMddHmmsss") + ".xls";
        boolean flag = ExcelUtils.getInstance().createExcel(fileName, rList);

        return new Result(flag, fileName);
    }
}
